package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: H_tao_X
 * Date: 2022-08-11
 * Time: 11:56
 */
@Slf4j
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    /**
     * 收藏音乐 模块
     */
    // 注入 lovemusicMapper
    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id,
                                                  HttpServletRequest request) {

        int musicId = Integer.parseInt(id);
        System.out.println("musicId：" + musicId);
        // 1. 检查当前是否登录了
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)
        == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"请登录后上传",false);
        }

        // 2. 如果登录了 获取里面的 user 信息
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);
        if(music != null) {
            // 说明之前收藏过这个音乐，不能再进行收藏!TODO:加一个取消收藏的功能
            return new ResponseBodyMessage<>(-1,"您之前收藏过这个音乐",false);
        }
        Boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
        if(effect) {
            return new ResponseBodyMessage<>(0,"收藏成功！",true);
        }else {
            return new ResponseBodyMessage<>(-1,"收藏失败1",false);
        }
    }

    /**
     * 查询喜欢的音乐，如果名字为空查询所有，如果名字不为空，根据userId和musicId查询
     * @param musicName
     * @param request
     * @return
     */
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request) {
        // 1. 检查是否登录
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null) {
            log.info("没有登录！");
            return new ResponseBodyMessage<>(-1,"请登录后再查找！",null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        log.info("userId: " + userId);

        // 2. 查询收藏的音乐
        List<Music> musicList = null;
        // 查询时：如果musicName 等于空，就去查询id
        if(musicName == null) {
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }else {
            // 如果 musicName 不等于空，根据 musicName和Id查询所有
            musicList = loveMusicMapper.findLoveMusicBykeyAndUID(musicName,userId);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有歌曲信息!",musicList);

    }

    /**
     * 实现 取消收藏音乐模块
     * 注意：当我删除库里面的音乐时候，lovemusic里面的也应该同步删除 post请求
     */

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,
                                                        HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        // 先检查是否登录
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null) {
            log.info("没有登录!");
            return new ResponseBodyMessage<>(-1,"请登录后，在进行上传！",false);
        }
        // 如果登录，通过httpSession 获取 user信息
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        log.info("userId: " + userId);
        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(ret == 1) {
            log.info("移除成功/取消收藏成功!");
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }else {
            return  new ResponseBodyMessage<>(-1,"取消收藏失败",false);
        }
    }


}
